package com.cjjd.c.wxapi;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.cjjd.c.App;
import com.cjjd.c.EventBusKeys;
import com.cjjd.c.utils.ToastUtil;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;

import org.greenrobot.eventbus.EventBus;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        App.WX_API.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {

    }

    @Override
    public void onResp(BaseResp resp) {

        /**
         * 微信支付回调
         */

        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("提示");
            builder.setMessage("" + resp.errCode);
            builder.show();
        }


        // 微信支付返回
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            Log.d("xyd", "onPayFinish,errCode=" + resp.errCode);
            switch (resp.errCode) {

                case BaseResp.ErrCode.ERR_OK:
                    EventBus.getDefault().post(EventBusKeys.hadPay);
                    break;
                case BaseResp.ErrCode.ERR_AUTH_DENIED:  // 拒绝
                case BaseResp.ErrCode.ERR_USER_CANCEL:  // 取消
                    ToastUtil.showToast("支付失败");
                    break;

            }
            finish();
            return;
        }
    }
}